﻿@{
    ViewBag.Title = "Gradebook";
    List<string> TabNames = ViewBag.TabNames;
    string SelectedTab = ViewBag.SelectedTab;
    List<List<string>> Table = ViewBag.TableData;
    bool GradeBookExists = ViewBag.GradeBookExists;
    bool IsInstructorOrTa = ViewBag.CanUpload;
    bool DisplaySearch = false;
    int NameColumnIndex = -1;
    List<int> hiddenColumnIndicies = ViewBag.Instructor_ColumnsToHide;
    if(ViewBag.NameColumnIndex != null)
    {
        DisplaySearch = true;
        NameColumnIndex = ViewBag.NameColumnIndex;
    }
    List<int> globalRowIndicies = ViewBag.GlobalRows;
}

<section data-tab="Grades">
    @if (IsInstructorOrTa)
    {
        <!--Upload Gradebook Form-->
        <form id="upload_gradebook" enctype="multipart/form-data" method="post" action="/Gradebook/UploadGradebook">
            <legend>@ViewBag.LastUploadMessage</legend>
            <select name="courseId">
                @foreach (OSBIDE.Library.Models.Course c in ViewBag.Courses)
                {
                    <option value="@c.Id">@c.Name</option>
                }
            </select>
            <input type="file" id="file" name="file"><br />
            <input id="upload" type="submit" value="Upload" disabled="disabled" />
            @{string helpLink = Url.Action("GradebookHelp", "Gradebook", new { area = "" });}
            <a style="font-size:small; text-decoration:none" href="@helpLink">Learn More...</a>
            <br />
            <text id="errorMsg">@ViewBag.UploadErrorMsg</text>
            <a href="~/Content/gradebook.zip">download Excel plugin</a>
        </form>
    }
    else
    {
        @:<text id="upload_gradebook">@ViewBag.LastUploadMessage</text>
    }


    <h1>@ViewBag.Title </h1>

    <article>
        <h2>Select Course</h2>
        @using (Html.BeginForm("Index", "Gradebook", FormMethod.Get))
        {
            <select name="courseId">
                @foreach (OSBIDE.Library.Models.Course c in ViewBag.EnrolledCourses)
                {
                    string selected = "";
                    if (c.Id == ViewBag.CurrentCourseId)
                    {
                        selected = "selected=\"selected\"";
                    }
                    <option @Html.Raw(selected) value="@c.Id">@c.Name</option>
                }
            </select>
            <input type="submit" value="View Grades" />
        }
    </article>

    @if (GradeBookExists == false)
    {
        <h3>No Gradebook Uploaded.</h3>
    }
    else
    {
        <h2>Grading Categories</h2>
        <ul id="GradebookNav">
            @foreach (string tabName in TabNames)
            {
                string liClasses = "GradebookNavItem";
                if (tabName == SelectedTab)
                {
                    liClasses += " GradebookNavActive";
                    <li class="@liClasses"><text>@tabName</text></li>
                }
                else
                {
                    string actionLink = Url.Action("Index", "Gradebook", new { area = "", gradebookName = tabName, courseId = ViewBag.CurrentCourseId });
                    liClasses += " GradebookNavNotActive";
                    <li class="@liClasses"><a href="@actionLink">@tabName</a></li>
                }
            }
        </ul>
        <br />
        <div>
            @if (DisplaySearch)
            {
                <span>Search by name:</span>
                <input id="search"></input>
                <br />
            }

            <!--table here-->
            <table id="Gradebook">
                @for (int i = 0; i < Table.Count; i++)
                {
                    string rowClass = "";
                    if (i == 0)
                    {
                        rowClass = "HeaderRow";
                    }

                    if (globalRowIndicies != null && globalRowIndicies.Contains(i))
                    {
                        rowClass += " GlobalRow";
                    }
                    <tr class="@rowClass">

                        @for (int j = 0; j < Table[i].Count; j++)
                        {
                            if (hiddenColumnIndicies == null || hiddenColumnIndicies.Contains(j) == false)
                            {
                                string tdId = "";
                                string tdClass = "";
                                if (DisplaySearch && j == NameColumnIndex)
                                {
                                    tdId = Table[i][j].ToLower();
                                    tdClass = "searchableName";
                                }

                                string displayValue = Table[i][j];
                                if (displayValue.Length > 0 && (displayValue[0] == '!' || displayValue[0] == '#'))
                                {
                                    displayValue = displayValue.Substring(1, displayValue.Length - 1);
                                }

                                if (globalRowIndicies.Contains(i) || i == 0)
                                {
                                    <th>@displayValue</th>
                                }
                                else
                                {
                                    <td class="@tdClass" id="@tdId">@displayValue</td>

                                }
                            }

                        }
                    </tr>
                }

            </table>
        </div>
    }

    <script type="text/javascript">

        $('#search').keyup(function (event) {
            if (event.keyCode == 13) {
                filterTable($('#search').val().toLowerCase());
            }

        });

        function filterTable(input) {
            $('.searchableName').each(function () {
                var parentRow = this.parentElement;
                if ($(parentRow).hasClass("GlobalRow") == false) {
                    $(parentRow).hide();
                }

            });

            var selector = ".searchableName[id*=\"" + input + "\"]'";
            $(selector).each(function () {

                var parentRow = this.parentElement;
                $(parentRow).show();
            });
        }

    </script>
</section>